package com.mobile.ado.setting.calendar;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

import com.mobile.ado.R;
import com.mobile.ado.contacts.Contact;
import com.mobile.ado.contacts.ContactsActivity;
import com.mobile.ado.util.message.Message;
import com.mobile.ado.util.message.MessageScheduler;
import com.mobile.ado.util.message.Message.MESSAGE_TYPE;

public class MessageScheduleActivity extends Activity implements
		OnTimeChangedListener, OnClickListener {

	private static final int REQUEST_CONTACT = 1;

	private TextView dayTextView;

	private TimePicker timePicker;
	private EditText messageEditText;
	private ImageButton selectContactImageButton;
	private EditText selectedContactEditText;

	private int year;
	private int month;
	private int day;

	private int hour;
	private int minute;

	private ArrayList<Contact> selectedContacts;

	private Calendar calendar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		selectedContacts = new ArrayList<Contact>();

		setContentView(R.layout.message_schedule);

		calendar = getSelectedDay();

		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH);
		day = calendar.get(Calendar.DATE);

		setupSelectedDayTextView();

		setupTimePicker();
		setupMessageEditText();
		setupSelectContactsButton();
		setupSelectedContactEditText();

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case REQUEST_CONTACT: {
			if (resultCode == RESULT_OK) {
				selectedContacts = (ArrayList<Contact>) data.getExtras().get(
						"selectedContacts");
				String name = "";
				for (int i = 0; i < selectedContacts.size(); i++) {
					name += selectedContacts.get(i).getName() + ";";
				}
				selectedContactEditText.setText(name);

				Message message = new Message(MESSAGE_TYPE.SMS, "This is a test");
				MessageScheduler scheduler = new MessageScheduler(this,
						selectedContacts, calendar, message);
				scheduler.startSchedule(false);
			}
			break;
		}
		}

	}

	private void setupSelectedContactEditText() {
		selectedContactEditText = (EditText) findViewById(R.id.selectedContact);
	}

	private void setupMessageEditText() {
		messageEditText = (EditText) findViewById(R.id.message);
	}

	private void setupSelectContactsButton() {
		selectContactImageButton = (ImageButton) findViewById(R.id.contact);
		selectContactImageButton.setOnClickListener(this);
	}

	private void setupTimePicker() {
		timePicker = (TimePicker) findViewById(R.id.timePicker);
		timePicker.setIs24HourView(true);
		timePicker.setOnTimeChangedListener(this);

		int hour = timePicker.getCurrentHour();
		int minute = timePicker.getCurrentMinute();
		calendar.set(Calendar.HOUR, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, 0);
	}

	private void setupSelectedDayTextView() {
		String dayLine = year + "年" + month + "月" + day + "日";

		dayTextView = (TextView) findViewById(R.id.day);
		dayTextView.setText(dayLine);
	}

	private Calendar getSelectedDay() {
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		Calendar calendar = null;
		if (extras == null) {
			calendar = Calendar.getInstance();
		}
		calendar = (Calendar) extras.getSerializable("selectedDay");
		return calendar;
	}

	@Override
	public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
		this.hour = hourOfDay;
		this.minute = minute;

		calendar.set(Calendar.HOUR, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, 0);
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(this, ContactsActivity.class);
		this.startActivityForResult(intent, REQUEST_CONTACT);
	}

}
